<?php

namespace app\forms\department;

use yii\db\Exception;
use yii\web\HttpException;
use app\models\Department;

/**
 * 部门 创建
 */
class CreateForm extends BaseForm
{

    /**
     * @param $param
     * @throws HttpException
     */
    public static function create($param)
    {
        $form = new self();
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';

        try {
            $attr = $form->getAttributes();
            $attr['group_id'] = $attr['groupId'];
            $attr['parent_id'] = $attr['parentId'];

            $model = new Department;
            $model->setAttributes($attr);
            $model->save(false);
        } catch (Exception $e) {
            $form->serverError();
        }

        $form->createOk();
    }
}